package com.ultimate.management.controller;

import com.ultimate.management.entity.LoginUser;
import com.ultimate.management.entity.SysMenu;
import com.ultimate.management.entity.vue.RouterVo;
import com.ultimate.management.util.MenuTree;
import com.ultimate.management.util.response.JwtResponse;
import com.ultimate.management.util.response.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LH
 * @Date 2024/2/27 13:58
 * @PackageName com.ultimate.management.controller
 * @Description: 菜单-权限处理类
 */
@RestController
@RequestMapping("/user")
public class SysMenuController {

    @Autowired
    private JwtResponse jwtResponse;

    @GetMapping("/getMenuRouterList")
    public JwtResponse getMenuRouterList(){
        System.out.println("getMenuRouterList进来了");
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //获取用户信息
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //获取对应的权限
        List<SysMenu> menuList = loginUser.getUser().getMenuList();
        //筛选目录和菜单
        List<SysMenu> collect = menuList.stream()
                .filter(item -> item != null && item.getType() != 2)
                .collect(Collectors.toList());
        //生成路由数据
        List<RouterVo> routerVos = MenuTree.makeRouter(collect,0L);
        return jwtResponse.ok().data("router",routerVos);
    }

    @GetMapping("/menuList")
    public JwtResponse getMenuList(){
        System.out.println("getMenuList进来了");
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //获取用户信息
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //获取对应的权限
        List<SysMenu> menuList = loginUser.getUser().getMenuList();
        //筛选目录和菜单
        List<SysMenu> collect = menuList.stream()
                .filter(item -> item != null && item.getType() != 2)
                .collect(Collectors.toList());
        //生成路由数据
        List<SysMenu> sysMenus = MenuTree.makeMenuTree(collect,0L);
        return jwtResponse.ok().data("menuList",sysMenus);
    }

}
